//
//  EXLoadingView.m
//  Edaox
//
//  Created by cao pan on 7/23/14.
//  Copyright (c) 2014 edaox. All rights reserved.
//

#import "EXLoadingView.h"
#import <QuartzCore/QuartzCore.h>


//////////////////////////////////////////////////////////////////////////////

@interface ProgressView : UIView {
	UIActivityIndicatorView*	_indicator;
	UILabel*					_messageLabel;
    UIView*                     _panelView;
}

// private methods
- (void)show;
- (void)dismiss;
- (void)setMessage:(NSString*)message;

@end


@implementation EXLoadingView


- (id)init {
	self = [super initWithFrame:CGRectZero];
	if (self) {
		_progressView = [[ProgressView alloc]initWithFrame:CGRectZero];
        [_progressView setAutoresizingMask:
         UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin];
	}
	return self;
}



- (void)didMoveToSuperview {
	if ([self superview]) {
		[_progressView setFrame:[[self superview] bounds]];
		[[self superview] addSubview:_progressView];
	}
}

- (void)show {
	// Makesure we are call this method in UI thread.
	if (![NSThread isMainThread]) {
		[self performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
		return;
	}
	// Put the progress view to top level of z-index.
	[[self superview] bringSubviewToFront:_progressView];
	[_progressView show];
}

- (void)dismiss {
	// Makesure we are call this method in UI thread.
	if (![NSThread isMainThread]) {
		[self performSelectorOnMainThread:@selector(dismiss) withObject:nil waitUntilDone:NO];
		return;
	}
	[_progressView dismiss];
}

- (void)setMessage:(NSString*)message {
	// Makesure we are call this method in UI thread.
	if (![NSThread isMainThread]) {
		[self performSelectorOnMainThread:@selector(setMessage:) withObject:message waitUntilDone:NO];
		return;
	}
	[_progressView setMessage:message];
}

@end

//////////////////////////////////////////////////////////////////////////////

@implementation ProgressView

- (id)initWithFrame:(CGRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
		self.alpha = 0.0;
		self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.1];
		self.autoresizesSubviews = NO;
		
        _panelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
        [_panelView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.4]];
        _panelView.layer.cornerRadius	= 10;
        _panelView.layer.masksToBounds	= YES;
        _panelView.clipsToBounds = NO;
        [self addSubview:_panelView];
        
		// Add message label
		_messageLabel = [[UILabel alloc] initWithFrame:CGRectZero];
		_messageLabel.backgroundColor	= [UIColor clearColor];
		_messageLabel.textColor			= [UIColor whiteColor];
		_messageLabel.shadowColor		= [UIColor blackColor];
		_messageLabel.shadowOffset		= CGSizeMake(0, -1);
		_messageLabel.opaque			= NO;
		_messageLabel.textAlignment		= NSTextAlignmentCenter;
		_messageLabel.font				= [UIFont boldSystemFontOfSize:13.0f];
		_messageLabel.adjustsFontSizeToFitWidth	= NO;
		[self addSubview:_messageLabel];
		
		_indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
		[_indicator setFrame:CGRectMake(0, 0, 32, 32)];
		[self addSubview:_indicator];
	}
	return self;
}



- (void)show {
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.30];
	// self.transform = CGAffineTransformConcat(CGAffineTransformIdentity, CGAffineTransformMakeScale(1.0, 1.0));
	self.alpha = 1.0;
	[UIView commitAnimations];
	
	[_indicator startAnimating];
}

- (void)dismiss {
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.30];
	// self.transform = CGAffineTransformConcat(CGAffineTransformIdentity, CGAffineTransformMakeScale(0.5, 0.5));
	self.alpha = 0.0;
	[UIView commitAnimations];
	
	[_indicator stopAnimating];
}

- (void)setMessage:(NSString*)message {
	_messageLabel.text = message;
	[self setNeedsLayout];
}

- (void) layoutSubviews {
	CGRect bounds = self.bounds;
    
    _panelView.center = self.center;
    
	CGRect frameInd = _indicator.bounds;
	frameInd.origin.x = floor((bounds.size.width - frameInd.size.width) / 2);
	frameInd.origin.y = floor((bounds.size.height - frameInd.size.height) / 2)-10;
	_indicator.frame = frameInd;
	
	// Get size of label text
	CGSize dims = [_messageLabel.text sizeWithFont:_messageLabel.font];
	int cxLabel = (bounds.size.width - dims.width) / 2;
	int cyLabel = frameInd.origin.y + 35;
	
	CGRect rectLabel = CGRectMake(cxLabel, cyLabel, (int)dims.width, (int)dims.height);
	_messageLabel.frame = rectLabel;
    
	[super layoutSubviews];
}


@end
